package TUILayer;
import java.util.Scanner;
import ControlLayer.DVDCtr;
import ModelLayer.DVD;
import ModelLayer.DVDCollections;
/**
 * User interface to the DVD controller
 * @author Gruppe A
 * @version 1.0
 */

public class DvdUI
{
    private DVDCtr dvdCtr;//DVD UI must know the DVD controller 

    /**
     * The constructor for the Dvd UI
     */
    public DvdUI()
    {
        dvdCtr = new DVDCtr();        
    }

    /**
     * The main method, starting the whole process within dvd UI
     */
    public void start()
    {
        dvdMenu();
    }

    /**
     * The method with executes the commands with in the DVD UI, it translates the input from our 
     * dvd menu into actual method calls
     * It also has some redundancy built into it, to prevent people from choosing numbers that aren't
     * listed in the program.
     */
    public void dvdMenu()
    {   
        boolean exit = false;
        while(!exit)
        {
            //show menu and return the user's choise
            int choice =   writeDVDMenu();
            if(choice != 1 && choice != 2 && choice !=3 && choice !=4 && choice != 5 && choice !=6)
            {
                System.out.println("You have to choose either 1, 2, 3, 4, 5 or 6");
            }
            if (choice == 1)
            {
                findDVD();
            }
            else{if(choice == 2){
                    createDVD();
                }
                else{
                    if(choice == 3){
                        createCopy();
                    }
                    else{
                        if(choice == 4){
                            deleteDVD();
                        }
                        else{
                            if(choice == 5){
                                listAllDvds();
                            }
                            else{
                                if(choice == 6)
                                {
                                    exit = true;
                                }
                            }//end else
                        }//end else
                    }//end else
                }//end else
            }//end else

        }//end while
    }

    /**
     * The method that prints the menu and also receives the input from the user and returns it for
     * further use, it has a while loop built into it, so that it can prevent people 
     * from using letters. But will instead present them an error message if they try
     * @returns int choice
     */
    private int writeDVDMenu()
    {
        // creates an object keyboard to read data from the keyboard
        Scanner keyboard = new Scanner(System.in);
        System.out.println(" *** DVD Menu ***");
        System.out.println(" (1) Show DVD information");
        System.out.println(" (2) Create DVD description");
        System.out.println(" (3) Create DVD specimen");
        System.out.println(" (4) Delete DVD");
        System.out.println(" (5) Show current DVD list");
        System.out.println(" (6) Return");
        System.out.print("\n Make your choice: ");

        int choice = 0;
        boolean ok = false;
        while(!ok)
        {
            try
            {
                choice = keyboard.nextInt();
                ok = true;
            }
            catch (Exception e)
            {
                System.out.println("You must enter a number - try again");
                String input = keyboard.nextLine();
            }

        }//end while
        return choice;
    }

    /**
     * A method for inputting a dvd no, it returns the number for further use in method calls.
     * It also has a while loop implemented that checks wether or not the input is valid number or if it's an invalid command, in which case they will be directed to using
     * the correct input method.
     * @return long no
     */
    private long inputDVDNo()
    {
        // creates an object keyboard to read data from the keyboard
        Scanner keyboard = new Scanner(System.in);
        System.out.println("Type DVD number: ");
        long no = 0;
        boolean ok = false;
        while(!ok)
        {
            try
            {
                no = keyboard.nextLong();
                ok = true;
            }
            catch (Exception e)
            {
                System.out.println("You must enter a number - try again");
                String input = keyboard.nextLine();
            }

        }//end while
        return no;
    }

    /**
     * A method for inputting a dvd title, it also returns it for further use in other methods
     * @return String title
     */
    private String inputTitle()
    {
        // creates an object keyboard to read data from the keyboard
        Scanner keyboard = new Scanner(System.in);
        System.out.println("Type DVD title: " );
        String title = keyboard.nextLine();
        return title;
    }

    /**
     * A method for inputting a dvd director, it also returns it for further use in other methods
     * @return String director
     */
    private String inputDirector()
    {
        // creates an object keyboard to read data from the keyboard
        Scanner keyboard = new Scanner(System.in);
        System.out.println("Type DVD director: " );
        String director = keyboard.nextLine();
        return director;
    }

    /**
     * A method for inputting a copies id number, it also returns it for further use in other methods
     *It also has a while loop implemented that checks wether or not the input is valid number or if it's an invalid command, in which case they will be directed to using
     * the correct input method.
     * @return long no
     */
    private long inputCopyNo()
    {
        // creates an object keyboard to read data from the keyboard
        Scanner keyboard = new Scanner(System.in);
        System.out.println("Type the copy number: ");
        long no = 0;
        boolean ok = false;
        while(!ok)
        {
            try
            {
                no = keyboard.nextLong();
                ok = true;
            }
            catch (Exception e)
            {
                System.out.println("You must enter a number - try again");
                String input = keyboard.nextLine();
            }

        }//end while
        return no;
    }

    /**
     * A method for inputting a dvd aquisition date, it also returns it for further use in other methods
     * @return String date
     */
    private String inputAquiringDate()
    {
        // creates an object keyboard to read data from the keyboard
        Scanner keyboard = new Scanner(System.in);
        System.out.println("Type the date of aquisition: ");
        String date = keyboard.nextLine();
        return date;
    }

    /**
     * A method for inputting a dvds cost, it also returns it for further use in other methods
     * It also has a while loop implemented that checks wether or not the input is valid number or if it's an invalid command, in which case they will be directed to using
     * the correct input method.
     * @return double cost
     */
    private double inputCost()
    {
        // creates an object keyboard to read data from the keyboard
        Scanner keyboard = new Scanner(System.in);
        System.out.println("Type the cost of the copy: ");
        double no = 0;
        boolean ok = false;
        while(!ok)
        {
            try
            {
                no = keyboard.nextDouble();
                ok = true;
            }
            catch (Exception e)
            {
                System.out.println("You must enter a number - try again");
                String input = keyboard.nextLine();
            }

        }//end while
        return no;
    }

    //there is need for more methods to read information about DVDs
    //and methods to display the information

    /**
     * A method for finding a specific dvd, it uses a few of our dvd input commands in order to find it.
     * After the specific dvd has been foudn it then prints the relevant information, if no dvd is found
     * it instead prints an error message
     */
    private void findDVD()
    {     
        long no = inputDVDNo();
        DVD d = dvdCtr.findDVD(no);
        //is to be changed when the DVD controller is completed
        if(d != null)
        {
            System.out.println("***The following is the information on the DVD***");
            System.out.println(" Title: "+d.getTitle());
            System.out.println(" Director: "+d.getDirector());
            System.out.println(" ID: "+Long.toString(d.getId()));
            System.out.println("\n");
        }
        else
        {
            System.out.println("\n");
            System.out.println("No DVDs exist");
            System.out.println("\n");
        }//end else
    }

    /**
     * A method for creating a dvd, it uses a few of our dvd input commands in order to provide all the relevant
     * information, after the dvd has been created it prints a confirmation message
     */
    private void createDVD()
    {
        String title = inputTitle();
        String director = inputDirector();
        long id = inputDVDNo();

        dvdCtr.createDVD(title, director, id);
        System.out.println("\n");
        System.out.println("The DVD has been created");
        System.out.println("\n");
    }

    /**
     * A method for creating a copy of a dvd, it uses a few of our dvd input commands in order to provide all the relevant
     * information, after the copy has been created it prints a confirmation message
     * It also checks if a dvd exist, if no dvd can be found with the specified id, it asks the user to create one
     */
    private void createCopy()
    {
        long id = inputDVDNo();
        long serialnr = inputCopyNo();
        String aquiringDate = inputAquiringDate();
        double cost = inputCost();
        DVD d = dvdCtr.findDVD(id);
        if(d != null)
        {
            dvdCtr.createCopy(d, serialnr, aquiringDate, cost);
            System.out.println("\n");
            System.out.println("The Copy has been created");
            System.out.println("\n");
        }
        else{
            System.out.println("\n");
            System.out.println("Create a DVD before you try to copy it");
            System.out.println("\n");
        }//end else

    }

    /**
     * A method for deleting a dvd from the system, it checks for the relevant dvd using our input methods
     * if one is found it deletes it and sends a confirmation message, if no dvd is found it prints an error message
     */
    private void deleteDVD()
    {
        long no = inputDVDNo();
        DVD d = dvdCtr.findDVD(no);
        if(d != null)
        {
            dvdCtr.deleteDVD(no);
            System.out.println("\n");
            System.out.println("The DVD has been deleted from the system");
            System.out.println("\n");
        }
        else
        {
            System.out.println("\n");
            System.out.println("Couldn't find the specified DVD");
            System.out.println("\n");
        }//end else
    }

    /**
     * A method for printing all our dvds and their copies
     */
    private void listAllDvds()
    {
        System.out.println("\n");
        System.out.println(dvdCtr.listAllDvds()+"\n");
    }
}